package com.app.service.Impl;

import cn.hutool.json.JSONUtil;
import com.app.common.RedisConstant;
import com.app.mapper.SysCategoryMapper;
import com.app.model.SysCategory;
import com.app.service.ISysCategoryService;
import com.app.service.RedisService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.app.util.FindChildrenUtil;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 公用数据配置	 服务实现类
 * </p>
 *
 * @author yoominic
 * @since 2023-12-05
 */
@Service
public class SysCategoryServiceImpl extends ServiceImpl<SysCategoryMapper, SysCategory> implements ISysCategoryService {
    @Autowired
    private RedisService redisService;



    @PostConstruct
    public void initCategoryCache() {
       initCategoryTree();
    }


    @Override
    public List<SysCategory> getCategoryTree(List<SysCategory> categoryList) {
        return FindChildrenUtil.findCategoryChildren(categoryList, 0);
    }

    @Override
    public void initCategoryTree() {
        QueryWrapper<SysCategory> sysCategoryQueryWrapper=new QueryWrapper<>();
        sysCategoryQueryWrapper.eq("status",1);
        sysCategoryQueryWrapper.orderByAsc("sort");
        List<SysCategory> categoryList = this.list(sysCategoryQueryWrapper);
        List<SysCategory> categoryTree = getCategoryTree(categoryList);
        this.redisService.set(RedisConstant.CATEGORY_CACHE_PREFIX, JSONUtil.toJsonStr(categoryTree));
    }


}
